import { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'

export function useVerifyCode(phoneModel: any) {
  const time = ref(60)
  let timer = 0
  const startTime = () => {
    timer = window.setInterval(() => {
      time.value--
      if (time.value <= 0) {
        clearInterval(timer)
        timer = 0
      }
    }, 1000)
  }
  const handleCodeBtn = () => {
    if (timer) {
      return
    }
    if (!phoneModel.phone) {
      ElMessage.warning('请输入手机号码')
      return
    }
    time.value = 60
    startTime()
  }
  const codeBtnText = computed(() => {
    if (time.value === 0) {
      return '重新获取'
    } else if (time.value === 60) {
      return '获取验证码'
    }
    return `${time.value}s`
  })
  return {
    handleCodeBtn,
    codeBtnText
  }
}
